package com.treasure.service.listener;

import com.treasure.service.listener.process.BizInformationPushForWechatOrSmsProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.treasure.service.constant.mq.GlobalConstant;
import com.treasure.service.dto.BizInformationPushForWechatOrSmsDto;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;

/**
* @Description:    业务处理推送消息至用户公众号
* @Author:         panxinwen
* @CreateDate:     2018/12/1 22:22
* @Version:        1.0
*/
@Component
public class BizInformationPushForWechatOrSmsListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BizInformationPushForWechatOrSmsProcess creditCardTransactionResultProcess;

    /**
     * 业务处理推送消息至用户公众号
     * @param bytes
     */
    @RabbitListener(queues = "${mq.config.biz-Information-PushForWechatOrSms-queue}", containerFactory = "rabbitListenerContainerFactory")
    public void bizInformationPushForWechatListener(@Payload byte[] bytes) {

        try {
            logger.info(" 接受mq消息，进入【业务处理推送消息至用户公众号】");
            String result = null;
            result = new String(bytes, GlobalConstant.ENCODING_UTF8);
            if(result != null ){
                logger.info("执行业务处理推送消息至用户公众号MQ数据{}", result);
                BizInformationPushForWechatOrSmsDto bizInformationPushForWechatDto =JSONObject.parseObject(result.toString(), BizInformationPushForWechatOrSmsDto.class);
                creditCardTransactionResultProcess.process(bizInformationPushForWechatDto);
            }else{
                logger.info("业务处理推送消息至用户公众号MQ数据为空", result);
                return;
            }

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

}
